#include "ValueSync.h"

CValueSync::CValueSync(void)
{
	pShm = NULL;
	pLock = NULL;
	lpBuf = NULL;
	nLength = 0;
}

CValueSync::CValueSync(const wstring& strName, int nLen)
	: nLength(nLen)
{
	pShm = new CShareMem();
	lpBuf = pShm->Open(strName + L"_Shm", nLength);

	pLock = new CLock(strName+L"_Lock");
}

CValueSync::~CValueSync(void)
{
	if(pShm) 
		delete pShm;
	if(pLock) 
		delete pLock;
}

bool CValueSync::GetValue( unsigned char* outData, int nLen )
{
	if(nLen > nLength) return false;

	pLock->Lock();
	memcpy(outData, lpBuf, nLen);
	pLock->Unlock();

	return true;
}

bool CValueSync::SetValue( unsigned char* inData, int nLen )
{
	if(nLen > nLength) return false;

	pLock->Lock();
	memcpy(lpBuf, inData, nLen);
	pLock->Unlock();

	return true;
}